<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="语音标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入语音标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="语音合成后地址" prop="voice" label-width="120px">
        <el-input
          v-model="queryParams.voice"
          placeholder="请输入语音合成后地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resource:voice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:voice:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="voiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账户名称" align="center" prop="shopName"/>
      <el-table-column label="语音标题" align="center" prop="title" />
      <el-table-column label="语音文案" align="center" prop="context" :show-overflow-tooltip="true"/>
      <el-table-column label="语音合成后地址" align="center" prop="voice" width="350px">
          <template slot-scope="scope">
              <audio id="audio" ref="audio" :src="scope.row.voice"  controls></audio>
          </template>

      </el-table-column>

      <el-table-column label="标签" algin="center" prop="tagList">
        <template slot-scope="scope">
          <div v-if="scope.row.tagList != null">
            <el-tag v-for="tag in scope.row.tagList" :key="tag">{{tag}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resource:voice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合成语音对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="创建者" prop="createTime">
          <el-input v-model="form.createTime" placeholder="请输入创建者" />
        </el-form-item> -->
        <!-- <el-form-item label="商户id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入商户id" />
        </el-form-item> -->
        <el-form-item label="商户" v-if="rolesKey == '00'">
          <el-select
            v-model="form.shopId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入搜索商户并选择"
            @change="changeShop"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="账户名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入账户名称" />
        </el-form-item> -->
        <el-form-item label="语音标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入语音标题" />
        </el-form-item>
        <el-form-item label="语音文案" prop="context">
          <el-input v-model="form.context" type="textarea" :maxlength="100" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
            <el-select v-model="form.tags"
                        multiple
                        placeholder="请选择">
            <el-option
                v-for="item in tagsOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="语音声音" prop="per">
          <el-select v-model="form.per" placeholder="请选择">
            <el-option
                v-for="item in perOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVoice, getVoice, delVoice, addVoice } from "@/api/resource/voice";
import { downLoadExcel } from "@/utils/download";
import { listUser } from '@/api/merchant/merchant'
import {selectList} from '@/api/resource/resource'
import { getSelectTags } from '@/api/tag/tag'
import { mapState } from 'vuex'
export default {
  name: "Voice",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合成语音表格数据
      voiceList: [],
      //标签列表
      tagsOptions: [],
      //商户标签
      shopList: [],
      //语音标签
      perOptions: [
        {dictValue: '潇颜', dictLabel: '潇颜 自然情感女声'},
        {dictValue: '知楠', dictLabel: '知楠 精品广告男声'},
        {dictValue: '猫小美', dictLabel: '猫小美 活力女声'},
        {dictValue: '柜姐', dictLabel: '柜姐 直播女声'},
        {dictValue: '潇轩', dictLabel: '潇轩-温和'},
        {dictValue: '婉贞', dictLabel: '婉贞 温柔四川女声'},
        {dictValue: '佳佳', dictLabel: '佳佳 粤语女声'},
        {dictValue: 'Abbie', dictLabel: 'Abbie 美式女声'},
        {dictValue: '潇悠', dictLabel: '潇悠 精品儿童女声'},
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: undefined,
        title: undefined,
        context: undefined,
        voice: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
   computed: {
    ...mapState({
      rolesKey: (state) => state.user.rolesKey,
    }),
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合成语音列表 */
    getList() {
      this.loading = true;
      listVoice(this.queryParams).then(response => {
        this.voiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取音乐标签
    getTags(id){
      getSelectTags({
        shopId:id
      }).then(res => {
        this.tagsOptions = res.data
      })
    },
    //远程搜索方法
    remoteMethod(query) {
      listUser({
        shopName: query,
      }).then((res) => {
        this.shopList = res.rows;
      });
    },
     //选中商户函数
    changeShop(e) {

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        shopId: undefined,
        title: undefined,
        context: undefined,
        voice: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTags()
      this.open = true;
      this.title = "添加合成语音";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          addVoice(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            }).finally(() => {
                this.buttonLoading = false;
                });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合成语音编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delVoice(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
      }).finally(() => {
          this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      downLoadExcel('/system/voice/export', this.queryParams);
    },
    /**音频播放按钮  */
    handlePlay(src) {

    },
  }
};
</script>
